package org.abacus.hibernate.outshineothers;

import org.abacus.hibernate.TransactionStrategy;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 * @author chaoyang.li
 */
public class HibernateTransactionStrategy extends org.abacus.hibernate.HibernateTransactionStrategy implements
		org.abacus.hibernate.TransactionStrategy {
	private static final ThreadLocal<TransactionStrategy> instance = new ThreadLocal<TransactionStrategy>();
	private Transaction tx;

	/**
	 * @author chaoyang.li
	 */
	private HibernateTransactionStrategy() {
		super();
	}

	/**
	 * @author chaoyang.li
	 */
	public static TransactionStrategy getInstance() {
		if (instance.get() == null) {
			instance.set(new HibernateTransactionStrategy());
		}
		return instance.get();
	}

	/**
	 * @author chaoyang.li
	 */
	public void begin() {
		try {
			tx = begin(HibernateSessionProvider.getCurrentSession());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @author chaoyang.li
	 */
	public void commit() {
		try {
			commit(tx);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @author chaoyang.li
	 */
	public void rollback() {
		try {
			rollback(tx);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @author chaoyang.li
	 */
	public void clearCache() {
		try {
			Session session = HibernateSessionProvider.getCurrentSession();
			clearCache(session);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
